package vn.appsmobi.ui;

import vn.appsmobi.R;
import android.content.Context;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExpandableTextView extends LinearLayout
{
  private Button mExpandButton;
  private OnExpandListener mExpandListener;
  private boolean mIsExpand;
  private boolean mIsMeasured;
  private int mLastHeight = -1;
  private int mLastWidth = -1;
  private float mLineSpacingExtra = 0.0F;
  private float mLineSpacingMultiplier = 1.0F;
  private int mMaxLines = -1;
  private CharSequence mText;
  private TextView mTextView;

  public ExpandableTextView(Context context)
  {
    super(context);
  }

  public ExpandableTextView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
  }


  private void initialize()
  {
    setOrientation(1);
    this.mTextView = ((TextView)findViewById(R.id.expandable_textview));
    this.mExpandButton = ((Button)findViewById(R.id.expand_button));
    this.mExpandButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        ExpandableTextView.this.expand(true);
        ExpandableTextView.this.mExpandButton.setVisibility(View.GONE);
        if (ExpandableTextView.this.mExpandListener != null)
          ExpandableTextView.this.mExpandListener.onExpand(ExpandableTextView.this);
      }
    });
  }

  private boolean isEllipsized()
  {
    
    if ((this.mIsExpand) || (this.mText == null) || (this.mTextView == null) || (this.mMaxLines < 0) || (this.mLastWidth < 0) || (this.mLastHeight < 0))
    	return true;
    else
    	if (new StaticLayout(this.mText, this.mTextView.getPaint(), this.mLastWidth - this.mTextView.getPaddingLeft() - this.mTextView.getPaddingRight(), Layout.Alignment.ALIGN_NORMAL, this.mLineSpacingMultiplier, this.mLineSpacingExtra, false).getLineCount() > this.mMaxLines)
    		return true;
    
    return false;
    
  }

  private void showExpandButton()
  {
    if (isEllipsized())
      this.mExpandButton.setVisibility(View.VISIBLE);
    else
      this.mExpandButton.setVisibility(View.GONE); 
  }

  public void expand(boolean isExpand)
  {
    this.mIsExpand = isExpand;
    TextView localTextView = this.mTextView;
    if (this.mIsExpand)
    	localTextView.setMaxLines(Integer.MAX_VALUE);
    else
    	localTextView.setMaxLines(this.mMaxLines);
  }

  protected void onFinishInflate()
  {
    super.onFinishInflate();
    initialize();
  }

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    this.mIsMeasured = true;
    LinearLayout.LayoutParams localLayoutParams = (LinearLayout.LayoutParams)this.mTextView.getLayoutParams();
    int width = getChildMeasureSpec(widthMeasureSpec, getPaddingLeft() + getPaddingRight(), localLayoutParams.width);
    int height = View.MeasureSpec.makeMeasureSpec(0, 0);
    measureChild(this.mTextView, width, height);
    if ((this.mTextView != null) && (this.mLastWidth != this.mTextView.getMeasuredWidth()) && (this.mLastHeight != this.mTextView.getMeasuredHeight()))
    {
      this.mLastWidth = this.mTextView.getMeasuredWidth();
      this.mLastHeight = this.mTextView.getMeasuredHeight();
      showExpandButton();
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

  public void setEllipsize(TextUtils.TruncateAt where)
  {
    this.mTextView.setEllipsize(where);
  }

  public void setExpandListener(OnExpandListener listener)
  {
    this.mExpandListener = listener;
  }

  public void setMaxLines(int maxLines)
  {
    this.mMaxLines = maxLines;
    this.mTextView.setMaxLines(this.mMaxLines);
  }

  public void setText(CharSequence text)
  {
    this.mText = text;
    this.mTextView.setText(text);
    if (this.mIsMeasured)
      showExpandButton();
  }
  
  public static abstract interface OnExpandListener
  {
    public abstract void onExpand(ExpandableTextView paramExpandableTextView);
  }
}